using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using Microsoft.Health.Samples.PatientConnect.TreyResearch.HealthVaultAdapter;
public partial class AddPatient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AddJavaScript();
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        // Create New Patient in Local Database
        string personID = ClinicalDataBaseManager.CreatePatient(Server.MapPath(@"Data\" + Session["PracticeFolderName"].ToString() + @"\" + Constants.PatientXmlFileName),
            TextBoxLastName.Text.Trim(), TextBoxFirstName.Text.Trim(), TextBoxMiddleName.Text.Trim());
        string identificationCode = string.Empty;
        if (personID != null)
        {
            String friendlyName = TextBoxFirstName.Text.Trim() + " " + TextBoxMiddleName.Text.Trim() + " " + TextBoxLastName.Text.Trim();
            Practice practice = new Practice(Path.Combine(Server.MapPath(@"Data"), Constants.PracticeXmlFileName));
            List<Practice> practiceList = new List<Practice>();
            practiceList = practice.SelectPracticeRecords("PracticeName='" + Session["ProviderPracticeName"] + "'");
            if (practiceList.Count > 0)
            {
                LabelErrorMessage.Text = string.Empty;
                PatientConnectUtilities healthVaultUtilities = new PatientConnectUtilities(practiceList[0].ChildAppId);

                identificationCode = healthVaultUtilities.CreatePatient(friendlyName, TextBoxIDQuestion.Text.Trim(), TextBoxIDAnswer.Text.Trim(), personID);

                ClinicalDataBaseManager.CreateXmlDataBase(Server.MapPath(@"Data\") + Session["PracticeFolderName"], personID,
                    Server.MapPath(Constants.SampleCcrCcdFolderName), Server.MapPath(Constants.SampleHL7MessageFolderName));
            }
            else
            {
                LabelErrorMessage.Text = "Practice not available in the practice list. Please login properly ";
            }
            Session["FriendlyName"] = friendlyName.Trim();

            Response.Redirect("Acknowledgement.aspx?IDcode=" + identificationCode + "&PersonID=" + personID);
        }
    }

    /// <summary>
    /// Add Java Scripts in Current page controls. 
    /// </summary>   
    private void AddJavaScript()
    {
        string junkChars = "<,>,&,\",(,),^,!,#,$,%,*,+,=,_,-";
        TextBoxFirstName.Focus();
        TextBoxFirstName.Attributes.Add("Onkeypress", "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxLastName.Attributes.Add("Onkeypress", "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxMiddleName.Attributes.Add("Onkeypress", "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxIDQuestion.Attributes.Add("Onkeypress", "CheckSpecialCharacter('" + junkChars + "')");
        TextBoxIDAnswer.Attributes.Add("Onkeypress", "CheckSpecialCharacter('" + junkChars + "')");

    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx", false);
    }
}
